package com.rambler.extension.controller;

import com.rambler.common.response.CommonResponse;
import com.rambler.extension.dto.ExtBookmarkDTO;
import com.rambler.extension.service.ExtBookmarkService;
import com.rambler.extension.vo.ExtBookmarkVO;
import com.rambler.api.fileupload.FileUploadProxy;
import com.rambler.api.fileupload.dto.FileUploadDTO;
import com.rambler.api.fileupload.dto.ImageProcessOption;
import com.rambler.api.fileupload.vo.FileUploadVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * Bookmark Controller
 * 负责书签资源的增删改查接口
 *
 * @author suhao
 */
@RestController
@Tag(name = "书签接口", description = "提供书签的创建、查询、更新、删除等功能")
public class ExtBookmarkController {

    @Resource
    private ExtBookmarkService bookmarkService;

    @GetMapping("/bookmark/{id}")
    @Operation(summary = "根据ID查询书签", description = "根据书签ID获取对应书签的详细信息")
    public CommonResponse<ExtBookmarkVO> getById(
            @Parameter(description = "书签ID", required = true)
            @PathVariable("id") String id) {
        return bookmarkService.queryById(id);
    }

    @GetMapping("/bookmarks")
    @Operation(summary = "查询用户所有书签", description = "根据用户ID查询其所有的书签列表")
    public CommonResponse<List<ExtBookmarkVO>> getAllBookmarks(
            @Parameter(description = "用户ID", required = true)
            @RequestParam("userId") String userId) {
        ExtBookmarkDTO bookmarkDTO = new ExtBookmarkDTO();
        bookmarkDTO.setUserId(userId);
        return bookmarkService.queryBookmarksByDTO(bookmarkDTO);
    }

    @DeleteMapping("/bookmark/{id}")
    @Operation(summary = "删除书签", description = "根据书签ID删除指定书签")
    public CommonResponse<Void> deleteBookmark(
            @Parameter(description = "书签ID", required = true)
            @PathVariable String id) {
        return bookmarkService.deleteBookmark(id);
    }

    @PostMapping("/bookmark")
    @Operation(summary = "创建书签", description = "根据请求体提供的信息新增一个书签")
    public CommonResponse<ExtBookmarkVO> createBookmark(
            @Parameter(description = "书签创建请求体", required = true)
            @RequestBody ExtBookmarkDTO bookmarkDTO) {
        return bookmarkService.createBookmark(bookmarkDTO);
    }

    @PutMapping("/bookmark")
    @Operation(summary = "更新书签", description = "根据请求体提供的信息修改现有书签信息")
    public CommonResponse<ExtBookmarkVO> editBookmark(
            @Parameter(description = "书签更新请求体", required = true)
            @RequestBody ExtBookmarkDTO bookmarkDTO) {
        return bookmarkService.editBookmark(bookmarkDTO);
    }

    @PostMapping(value = "/bookmark/uploadIcon", consumes = "multipart/form-data")
    @Operation(summary = "上传书签图标", description = "上传书签图标")
    public CommonResponse<FileUploadVO> uploadIcon(
            @Parameter(description = "待上传的文件", required = true)
            @RequestPart MultipartFile file,
            @Parameter(description = "书签地址", required = true)
            @RequestParam String bookmarkUrl) {
        if (null == file) {
            return CommonResponse.failure("未指定上传文件");
        }
        if (null == bookmarkUrl || bookmarkUrl.isEmpty()) {
            return CommonResponse.failure("未指定书签地址");
        }
        return bookmarkService.uploadBookmarkIcon(file, bookmarkUrl);
    }
}
